package com.heima.wemedia.controller.v1;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.WmUserThreadLocal;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController {

    @Autowired
    private WmNewsService wmNewsService;


    @PostMapping("/list")
    public ResponseResult findAll(@RequestBody WmNewsPageReqDto dto) {
        //获取用户信息
        WmUser user = WmUserThreadLocal.getUser();
        ResponseResult result = wmNewsService.findAll(dto, user);
        return result;
    }

    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto dto) {
        //获取用户信息
        WmUser user = WmUserThreadLocal.getUser();
        ResponseResult result = wmNewsService.submitNews(dto, user);
        return result;
    }

    @GetMapping("/one/{newsId}")
    public ResponseResult findOne(@PathVariable("newsId") Integer newsId) {
        //获取用户信息
        WmUser user = WmUserThreadLocal.getUser();
        return wmNewsService.findOne(newsId, user);
    }

    @GetMapping("/del_news/{newsId}")
    public ResponseResult delNews(@PathVariable("newsId") Integer newsId) {
        //获取用户信息
        WmUser user = WmUserThreadLocal.getUser();
        return wmNewsService.delNews(newsId, user);
    }

    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }

}